<?php
namespace app\admin\controller;
use think\Controller;

Class Order extends BaseAdmin
{

	//订单列表
    public function lst()
    {
        //查询条件
        $where=array();
    	if(request()->isPost()){
            $data=input('post.');

            if(isset($data['out_trade_no'])){
                $where['out_trade_no']=$data['out_trade_no'];//按订单编号查询
            }else{
                $where['order_status']=$data['order_status'];//联合查询
                $where['pay_status']=$data['pay_status'];
                $where['post_status']=$data['post_status'];
                $where['payment']=$data['payment'];// dump($data);die;
            }
            
        }else{
            $where=1;
        }
        
        $OrderRes=db('tp_order')->alias('o')->field('o.*,u.username')->join('tp_user u','u.id=o.user_id')->where($where)->paginate(10);
        $this->assign('OrderRes',$OrderRes);
        // dump($data->render());
        // exit;
        return $this->fetch('list');
    }

    //订单查询
    public function search(){
        return view();
    }

    //订单商品列表
    public function goodsList($id){
        $OrderRes=db('tp_order_goods')->where(array('order_id'=>$id))->select();
        // dump($OrderRes);die;
        $this->assign('OrderRes',$OrderRes);
        return view();
    }
    //订单商品的删除
    public function goodsDel($id){
        $OrderId=db('tp_order_goods')->field('order_id')->where(array('id'=>$id))->find();
        $del=db('tp_order_goods')->delete($id);
        if($del){
            $this->success('删除成功',url("Order/goodsList",array('id'=>$OrderId['order_id'])));

        }else{
            $this->error('删除失败');
        }

    }
    //订单商品的编辑
    public function goodsEdit($id){
        $OrderId=db('tp_order_goods')->field('order_id')->where(array('id'=>$id))->find();
        $OrderRes=db('tp_order_goods')->where(array('id'=>$id))->find();
        $OrderRes['goods_attr_str']=str_replace('<br/>', ';', $OrderRes['goods_attr_str']);
        $this->assign('OrderRes',$OrderRes);
        if(request()->isPost()){
            $data=input('post.');

            // dump($data);die;
            
            $up=db('tp_order_goods')->where(array('id'=>$id))->update($data);
            if($up!==false){
                $this->success('修改订单商品成功',url("Order/goodsList",array('id'=>$OrderId['order_id'])));
            }
            else{
                $this->error('修改订单商品失败');
            }

        }
        return view();
    }

    
    //订单编辑
    public function edit($id)
    {
    	$OrderRes=db('tp_order')->where(array('id'=>$id))->find();
    	$this->assign('OrderRes',$OrderRes);
    	if(request()->isPost()){
        	$data=input('post.');

            // dump($data);die;
        	
        	$up=db('tp_order')->where(array('id'=>$id))->update($data);
        	if($up!==false){
        		$this->success('修改订单成功','lst');
        	}
        	else{
        		$this->error('修改订单失败');
        	}

        }
        return $this->fetch();
    }

    //订单删除
    public function del($id)
    {
        $OrderGoodsRes=db('tp_order_goods')->where(array('order_id'=>$id))->select();
        if($OrderGoodsRes){
            db('tp_order_goods')->where(array('order_id'=>$id))->delete();
        }
    	$del=db('tp_order')->delete($id);
    	if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }

    
   
		
		
	


}